package com.realone.packman.codec.decoder;

import com.realone.packman.codec.decoder.vo.BmsDecoderDataVo10;
import com.realone.packman.codec.exception.BmsDecoderException;
import com.realone.packman.codec.vo.Cell;
import com.realone.packman.codec.vo.CellChargeState;
import com.realone.packman.codec.vo.CellTemperature;

import java.nio.ByteBuffer;

/**
 * 获取BMS所有状态和数据 解码
 * @author hugx
 * @create 2018-09-12 16:16
 */
public class BmsDecoder10 extends BmsDecoder<BmsDecoderDataVo10> implements BmsDecoderStrategy{

    private static final byte Not_Compress_Handler_Way=0;  //报文未压缩处理
    private static final byte Compress_Handler_Way=1;  //报文压缩处理

    public BmsDecoder10(Integer classType) {
        this.classType=classType;
    }

    @Override
    public BmsDecoderDataVo10 decoderBody(ByteBuffer bb) {
        if(bb == null){
            throw new BmsDecoderException(new NullPointerException());
        }

        Cell cell=new Cell();
        BmsDecoderDataVo10 result=new BmsDecoderDataVo10(this.classType,cell);

        /* 报文压缩标记，0-未压缩，1-报文经过了压缩； */
        byte hadlerWay=bb.get();
        if(Not_Compress_Handler_Way == hadlerWay){
            notCompressHandler(bb,cell);
        }else if(Compress_Handler_Way == hadlerWay){
            compressHandler(bb,cell);
        }

        return result;
    }

    /**
     * 报文未压缩处理
     * @param bb
     * @param cell
     */
    private void notCompressHandler(ByteBuffer bb,Cell cell){

        /*解析电池电压*/
        for(int i=0;i<14;i++){
           cell.addVoltage(decoderVoltage(bb.getShort()));
        }

        /* 解析电池包电压 */
        cell.setCellPackVoltage(decoderVoltage(bb.getShort()));

        /* 电池包充放电状态和电流 */
        decoderCellRecharge(bb,cell);

        /* 解析电池包充电均衡状态位图 */
        decoderCellChargeState(bb,cell);

        /* 解析电池包容量百分比*/
        cell.setCapacity(bb.get());

        /* 解析6个温度探测点 */
        decoderCellTemperature(bb,cell);

        /* 解析电池仓湿度值 */
        cell.setHumidity(bb.get() & 0xFF);

        /* 解析电池仓温度值 */
        cell.setTemperature((char) bb.get());
    }



    /**
     * 报文压缩处理
     * @param bb
     * @param cell
     */
    private void compressHandler(ByteBuffer bb,Cell cell){

    }

    private double decoderVoltage(short voltage){
        byte hight= (byte) (voltage >>> 8);
        short low= (short) (voltage & 0x00FF);
        double result=(hight * 256 + low) / 100.0;
        return result;
    }

    private void decoderCellChargeState(ByteBuffer bb, Cell cell){
        short cellChargeState=bb.getShort();
        for(int i=1;i<=16;i++){
             cell.addCellChargeState(new CellChargeState(i,(cellChargeState >>> (i-1)) & 1));
        }
    }

    private void decoderCellRecharge(ByteBuffer bb, Cell cell){
        short tmp=bb.getShort();
        cell.setRechargeState(tmp >>> 13);
        short electricitySizeTmp= (short) (tmp & 0x3FFF);
        byte hight= (byte) (((electricitySizeTmp >>> 8) & 0x3F) * 256);
        short low= (short) (electricitySizeTmp & 0x00FF);
        double electricitySize= (hight + low) / 20;
        cell.setElectricitySize(electricitySize);
    }

    private void decoderCellTemperature(ByteBuffer bb, Cell cell) {
        CellTemperature temperature=cell.getCellTemperature();
        if(temperature == null){
            temperature=new CellTemperature();
            cell.setCellTemperature(temperature);
        }

        temperature.setOne((char) bb.get());
        temperature.setTwo((char) bb.get());
        temperature.setThree((char) bb.get());
        temperature.setFour((char) bb.get());
      //TODO 暂时注掉  temperature.setFive((char) bb.get());
        temperature.setSix((char) bb.get());
    }

}
